import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtMultimedia 5.4

Window {
    id: root
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    color: "green"

    Rectangle {
        id: video
        anchors.fill: parent
        layer.enabled: true
        // textureSize好像和grabtoimage的size没啥关系
        // grabtoimage也不会从这个纹理上直接取数据，还是会每次自己创建纹理
        //layer.textureSize: "1920x1080"

        Camera {
            id: camera1
            captureMode: Camera.CaptureStillImage

            videoRecorder {
                resolution: "640x480"
                frameRate: 10
            }
        }

        Camera {
            id: camera2
            captureMode: Camera.CaptureStillImage

            videoRecorder {
                resolution: "640x480"
                frameRate: 10
            }
        }

        VideoOutput {
            id: viewfinder1
            visible: true
            anchors.fill: parent

            fillMode: VideoOutput.Stretch

            source: camera1

            Component.onCompleted: {
                camera1.start();
            }
        }

        VideoOutput {
            id: viewfinder2
            visible: true
            x: 0
            y: parent.height - height
            width: 160
            height: 120

            fillMode: VideoOutput.Stretch

            source: camera2

            Component.onCompleted: {
                camera2.start();
            }
        }

        Button {
            anchors.horizontalCenter: parent.horizontalCenter
            text: "save"
            onClicked: {
                console.time("grabToImage")
                // 25ms左右
                video.grabToImage(function(result) {
                    console.timeEnd("grabToImage")
                    result.saveToFile("/Users/barry/video.png");
                }, Qt.size(1920, 1080));
            }
        }
    }
}
